package com.wiz.web.manager.fatory;

import java.text.SimpleDateFormat;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.wiz.common.Constant;
import com.wiz.common.LamicConstant;
import com.wiz.vo.criteria.PayCriteria;
import com.wiz.vo.to.lamicpay.LamicBaseData;
import com.wiz.vo.to.lamicpay.LamicReqData;
import com.wiz.vo.to.lamicpay.LamicResData;
import com.wiz.vo.type.PayMethodType;
import com.wiz.web.manager.HttpManager;

/**
 * 获取支付宝数据模板
 * @author Hakeny
 */
public class LamicDataFactory implements LamicConstant,Constant{
	private transient final Logger log = LoggerFactory.getLogger(getClass());
	
	@Resource
	private HttpManager httpManager;
	@Resource
	private Gson gson;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");

	public void init() throws Exception{
		System.out.println("---------init-------");
		String date = sdf.format(new Date());
		LamicReqData reqData = new LamicReqData();
		String LAMIC_ENCRYPT_UID = LAMIC_UID+"<reqtime>"+date;
		String result =  httpManager.getLamicEncryptResult(LAMIC_ENCRYPT_UID);
		net.sf.json.JSONObject jsonObject = net.sf.json.JSONObject.fromObject(result);
//		reqData.setUid(DesUtil.encrypt(LAMIC_UID+"<reqtime>"+date,LAMIC_KEY));
		reqData.setUid(String.valueOf(jsonObject.get("resultMsg")));
//		reqData.setPwd(DesUtil.encrypt(LAMIC_PWD+"<reqtime>"+date,LAMIC_KEY));
		result =  httpManager.getLamicEncryptResult(LAMIC_PWD+"<reqtime>"+date);
		jsonObject = net.sf.json.JSONObject.fromObject(result);
		reqData.setPwd(String.valueOf(jsonObject.get("resultMsg")));
		result = httpManager.sendLamicPost(LAMIC_IP, LAMIC_LOGIN, reqData);
		System.out.println("======="+result);
		LamicResData resData = gson.fromJson(result, new TypeToken<LamicResData>(){}.getType());
		if(resData == null || StringUtils.isBlank(resData.getData().getToken())){
			System.out.println("error!!!!!!!!!!!!!!!!!!!!!!!!!!");
			log.error("Lamic login fail,reason="+resData);
		}else{
//			LAMIC_TOKEN = resData.getData().getToken();
		}
	}
	
	/**
	 * 根据支付方式不同选择不同data模板
	 * @param criteria
	 * @param type
	 * @return
	 * @throws Exception 
	 */
	public LamicBaseData getDataByType(PayCriteria criteria,int type,
			String lamicId,String lamicToken) throws Exception{
		if(PayMethodType.BAR_PAY.getId() == type){
			return getBarReqData(criteria,lamicId,lamicToken);
		}
		if(PayMethodType.QUERY.getId() == type){
			return getQueryReqData(criteria,lamicId,lamicToken);
		}
		if(PayMethodType.REVERSE.getId() == type){
			return getReverseReqData(criteria,lamicId,lamicToken);
		}
		return null;
	}
	
	/**
	 * 扫码支付请求数据
	 * @param criteria
	 * @return
	 * @throws Exception 
	 */
	private LamicReqData getBarReqData(PayCriteria criteria,String lamicId,String lamicToken) throws Exception{
//		String date = sdf.format(new Date());
		LamicReqData reqData = new LamicReqData();
//		reqData.setUid(DesUtil.encrypt(LAMIC_UID+"<reqtime>"+date,LAMIC_KEY));
		String date = sdf.format(new Date());
		reqData.setUid(lamicId+"<reqtime>"+date);
		reqData.setToken(lamicToken);
		reqData.setCode(criteria.getAuthCode());
		reqData.setMoney(String.valueOf(criteria.getTotalFee()/100.00));
		return reqData;
	}
	
	/**
	 * 查询请求数据
	 * @param criteria
	 * @return
	 * @throws Exception
	 */
	private LamicReqData getQueryReqData(PayCriteria criteria,String lamicId,String lamicToken) throws Exception{
//		String date = sdf.format(new Date());
		LamicReqData reqData = new LamicReqData();
//		reqData.setUid(DesUtil.encrypt(LAMIC_UID+"<reqtime>"+date,LAMIC_KEY));
		String date = sdf.format(new Date());
		reqData.setUid(lamicId+"<reqtime>"+date);
		reqData.setToken(lamicToken);
		reqData.setOut_trade_no(criteria.getTransactionId());
		return reqData;
	}
	
	/**
	 * 撤销请求数据
	 * @param criteria
	 * @return
	 * @throws Exception
	 */
	private LamicReqData getReverseReqData(PayCriteria criteria,String lamicId,String lamicToken) throws Exception{
//		String date = sdf.format(new Date());
		LamicReqData reqData = new LamicReqData();
//		reqData.setUid(DesUtil.encrypt(LAMIC_UID+"<reqtime>"+date,LAMIC_KEY));
		String date = sdf.format(new Date());
		reqData.setUid(lamicId+"<reqtime>"+date);
		reqData.setToken(lamicToken);
		reqData.setOrderid(criteria.getTransactionId());
		return reqData;
	}
}
